//
//  NSString+PriceHandle.m
//  TongGuanLease
//
//  Created by 张奇 on 2022/4/26.
//

#import "NSString+PriceHandle.h"

@implementation NSString (PriceHandle)

/// 价格处理，整数部分大 小数部分小
- (NSMutableAttributedString *)priceStringBig:(UIFont *)big small:(UIFont *)small bigColor:(UIColor *)bigColor smallColor:(UIColor *)smallColor {
    if (self && self.doubleValue >= 0) {
        NSString *price = self;
        NSString *str = [NSString stringWithFormat:@"￥%@", price];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str];
        if ([price containsString:@"."]) {
            NSArray<NSString *> *priceArr = [price componentsSeparatedByString:@"."];
            [attStr addAttributes:@{NSFontAttributeName : small, NSForegroundColorAttributeName : bigColor} range:NSMakeRange(0, 1)];
            [attStr addAttributes:@{NSFontAttributeName : big, NSForegroundColorAttributeName : bigColor} range:NSMakeRange(1, priceArr.firstObject.length)];
            [attStr addAttributes:@{NSFontAttributeName : small, NSForegroundColorAttributeName : smallColor} range:NSMakeRange(1 + priceArr.firstObject.length, priceArr.lastObject.length + 1)];
        }else {
            [attStr addAttributes:@{NSFontAttributeName : small, NSForegroundColorAttributeName : bigColor} range:NSMakeRange(0, str.length - price.length)];
            [attStr addAttributes:@{NSFontAttributeName : big, NSForegroundColorAttributeName : bigColor} range:NSMakeRange(str.length - price.length, price.length)];
        }
        return attStr;
    } else {
        return [[NSMutableAttributedString alloc] initWithString:@""];
    }
}


@end
